package com.jd.mysql.mgr.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jd.mysql.mgr.dao.MgrMachineDAO;
import com.jd.mysql.mgr.pojo.MgrMachine;

public class MgrMachineDAOImpl extends HibernateDaoSupport implements
		MgrMachineDAO {

	public void deleteMgrMachine(MgrMachine mgrm) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.delete(mgrm);
	}

	public MgrMachine findMgrMachineById(Integer id) {
		// TODO Auto-generated method stub
		return (MgrMachine) getHibernateTemplate().get(MgrMachine.class, id);
	}

	public void saveMgrMachine(MgrMachine mgrm) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.save(mgrm);
	}

	public void updateMgrMachine(MgrMachine mgrm) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.update(mgrm);
	}

	public List<MgrMachine> getMgrMachineList(Integer start, Integer limit) {
		// TODO Auto-generated method stub
		String hql = "from MgrMachine where yn=true order by id desc";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List<MgrMachine> list = query.list();
		return list;
	}

	public void disableMgrMachine(MgrMachine mgrmachine) {
		// TODO Auto-generated method stub
		mgrmachine.setYn(false);
		this.updateMgrMachine(mgrmachine);
	}

	public Long getTotalCount(Integer groupId,Boolean enable) {
		// TODO Auto-generated method stub
		String hql = "select count(*) as count from MgrMachine where 1=1 ";
		if(enable){
			hql += "and yn=true ";
		}
		if(groupId!=-1){
			hql += "and groupId="+groupId;
		}
		Long totalCount = 0l;
		Session session = getSession();
		Query query = session.createQuery(hql);
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			totalCount = list.get(0);
		}
		return totalCount;
	}

	public List<MgrMachine> getMgrMachineList(Integer start, Integer limit,
			Boolean yn, String ip,Integer groupId) {
		String hql = "from MgrMachine where 1=1 ";
		if(yn){
			hql+="and yn=true ";
		}
		if(ip!=null){
			hql+="and ip like '%"+ip+"%' " ;
		}
		if(groupId!=-1){
			hql += "and groupId="+groupId;
		}
		hql+="order by id desc ";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List<MgrMachine> list = query.list();
		return list;
	}

	public MgrMachine getMgrMachineByIp(String ip) {
		// TODO Auto-generated method stub
		String hql = "from MgrMachine where ip=:ip";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setString("ip", ip);
		List<MgrMachine> list = query.list();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public Long getTotalCount(Integer groupId, Boolean enable, String ip) {
		// TODO Auto-generated method stub
		String hql = "select count(*) as count from MgrMachine where 1=1 ";
		if(enable){
			hql += "and yn=true ";
		}
		if(ip!=null){
			hql+="and ip like '%"+ip+"%' " ;
		}
		if(groupId!=-1){
			hql += "and groupId="+groupId;
		}
		Long totalCount = 0l;
		Session session = getSession();
		Query query = session.createQuery(hql);
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			totalCount = list.get(0);
		}
		return totalCount;
	}

}
